package project.suntray.com.core.activitys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ContentFrameLayout;

import me.yokeyword.fragmentation.ISupportActivity;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportActivityDelegate;
import project.suntray.com.core.R;
import project.suntray.com.core.delegates.NatteDelagate;

/**
 * Created by zhangyang on 2017/12/12 14:52.
 * version 1
 * notes: 主要是继承 supportActivity 类
 */
public abstract  class ProxyActivity extends SupportActivity{

    //创建一个连接器
    SupportActivityDelegate delegate=new SupportActivityDelegate(this);

    /**
     * 获取delagate 将某个id ContentFrameLayout 与 fragment 进行绑定!
     * @return
     */
    protected   abstract NatteDelagate setDelagate();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContainer(savedInstanceState);
    }

    /**
     * 初始化容器
     * @param savedInstanceState
     */
    private void initContainer(Bundle savedInstanceState) {
        //创建frameLayout
        ContentFrameLayout contentFrameLayout=new ContentFrameLayout(this);
        //进行设置id
        contentFrameLayout.setId(R.id.container_id);
        setContentView(contentFrameLayout);
        //如果是首次进入 savedInstanceState 为空!
        if(savedInstanceState==null){
            //将对应的id 和 fragment进行绑定!
            delegate.loadRootFragment(R.id.container_id,setDelagate());
        }
    }

    /**
     * 界面消失
     */
    @Override
    protected void onDestroy() {
        delegate.onDestroy();
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }
}
